<?php

include_once 'travel_class.php';
include_once ('db_manage.php');

function getTravelArray($sql) {    
    //	echo $sql;
    //	echo "<br>";
    $travel_array = array();
    $result = mysql_query($sql);
    $i = 0;
    while (($rs = mysql_fetch_object($result)) != false) {
        $travel = new Travel ();

        $travel->setId($rs->id);
        $travel->setFid($rs->fid);
        $travel->setTitle($rs->title);
        $travel->setAddress($rs->address);
        $travel->setArea($rs->area);
        $travel->setContent($rs->content);
        $travel->setView($rs->view);
        $travel->setBed($rs->bed);
        $travel->setTraffic($rs->traffic);
        $travel->setExpense($rs->expense);
        $travel->setNum($rs->num);
        $travel->setHot($rs->hot);
        $travel->setMark($rs->mark);
        $travel->setType($rs->type);
        $travel->setCostdate($rs->costdate);
        $travel->setBackdate($rs->backdate);
        $travel->setState($rs->state);
        $travel->setReason($rs->reason);
        $travel->setPostuser($rs->postuser);
        $travel->setTimestamp($rs->timestamp);

        $travel_array [$i] = $travel;
        $i++;
    }
    return $travel_array;
}

function updateTravelHot($id) {
    $sql = "update pcd_travel set hot = hot +1 where id = " . $id;
    mysql_query($sql);
}

function getHotestTravel($num) {
    $sql = "SELECT * FROM pcd_travel ORDER BY HOT DESC LIMIT 0," . $num;
    return fetchData($sql);
}

function getTravelById($trvaleid) {
    $sql = "select * from pcd_travel where id = " . $trvaleid;
    $array = getTravelArray($sql);
    if (count($array) > 0) {
        return $array [0];
    }
}

function getAllTravelByPage($page, $num_row_of_page) {
    $start = $page * $num_row_of_page;
    $sql = "select * from pcd_travel order by timestamp desc limit " . $start . ", " . $num_row_of_page;
    return getTravelArray($sql);
}

function getAllTravelCount() {
    $sql = "select count(*) as total from pcd_travel where id = fid and id > 0 and state = 1";
    $result = mysql_query($sql);
    return mysql_num_rows($result);
}

function getTravelSearchSql($area, $title, $low, $high, $hot, $type, $isAdmin) {
    if (!isset($isAdmin)) {
        $isAdmin = - 1;
    }
    if ($title != '' || $low != '' || $high != '') {
        if ($isAdmin == 1) {
            $sql = "select * from pcd_travel where id > 0";
        } else if ($isAdmin == 0) {
            $sql = "select * from pcd_travel where id > 0 and stat = 0";
        } else {
            $sql = "select * from pcd_travel where id > 0 and state = 1";
        }
    } else {
        if ($isAdmin == 1) {
            $sql = "select * from pcd_travel where id > 0 and id = fid";
        } else if ($isAdmin == 0) {
            $sql = "select * from pcd_travel where id > 0 and id = fid and state = 0";
        } else {
            $sql = "select * from pcd_travel where id > 0 and id = fid and state = 1";
        }
    }
    if ($area != - 1) {
        $area_sql = " and area = " . $area . " ";
        $sql = $sql . $area_sql;
    }

    if ($title != "") {
        $title_sql = " and title like '%" . $title . "%' ";
        $sql = $sql . $title_sql;
    }

    if ($low != "") {
        $low_sql = " and expense >= " . $low . " ";
        $sql = $sql . $low_sql;
    }

    if ($high != "") {
        $high_sql = " and expense <= " . $high . " ";
        $sql = $sql . $high_sql;
    }

    if ($type != "" && $type != - 1) {
        $type_sql = " and type = " . $type . "";
        $sql = $sql . $type_sql;
    }

    if ($hot != "" && $hot != - 1) {
        $hot_sql = " order by hot " . $hot . " ";
        $sql = $sql . $hot_sql;
    } else {
        $hot_sql = " order by id desc ";
        $sql = $sql . $hot_sql;
    }
    return $sql;
}

function getSearchTravelResult($area, $title, $low, $high, $hot, $type, $page, $num_row_of_page, $isAdmin) {
    $start = $page * $num_row_of_page;
    $sql = getTravelSearchSql($area, $title, $low, $high, $hot, $type, $isAdmin);
    $sql = $sql . " limit " . $start . ", " . $num_row_of_page;
    return getTravelArray($sql);
}

function getSearchTravelCount($area, $title, $low, $high, $hot, $type, $isAdmin) {
    $sql = getTravelSearchSql($area, $title, $low, $high, $hot, $type, $isAdmin);
    $result = mysql_query($sql);
    return mysql_num_rows($result);
}

function getTravelAvgMark() {
    $avgmark = array();
    $sql = "select fid, avg(mark) as avgmark from pcd_mark group by fid";
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $id_mark = $rs->fid;
        $avg_mark = $rs->avgmark;
        $avgmark [$id_mark] = round($avg_mark, 1);
    }
    return $avgmark;
}

function getPicOfTravel() {
    $travel_pic = array();
    $sql = "select * from pcd_upload_travel";
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $src = $rs->src;
        $travelid = $rs->fid;
        $travel_pic [$travelid] = $rs->src_small;
    }
    return $travel_pic;
}

function getFacePicOfTravel() {
    $travel_face = array();
    $sql = "select src_small from pcd_photo_face";
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $travelid = $rs->albumid;
        $facepic = $rs->src_small;
        $travel_face [$travelid] = $facepic;
    }
    return $travel_face;
}

// add scence
function addScence($travel) {
    //array(addslashes($title),addslashes($content),addslashes($discuz_uid),$fid);
    $sql = "insert into pcd_travel(fid,title,state,costdate,content,postuser,hot,timestamp,type) values('" . $travel ['fid'] . "','" . $travel ['title'] . "'," . $travel ['state'] . ",now(),'" . $travel ['content'] . "','" . $travel ['postuser'] . "'," . $travel ['hot'] . ",now()," . $travel ['type'] . ")";
    mysql_query($sql);
    $lastId = mysql_insert_id();
    $sql = "update pcd_travel set fid = " . $travel ['fid'] . " where id = " . $travel ['fid'];
    mysql_query($sql);
    $sql = "update pcd_travel_route set travelid = " . $lastId . ",type='scence' where travelid = 0 and travelname = '" . $travel ['title'] . "'";
    mysql_query($sql);
    return $lastId;
}

function addTravel($travel) {
    //$travel = array($title,$area,$view,$bed,$traffic,$expense,$num,$costdate,$content,$discuz_uid);
    //$sql = "insert into pcd_travel values(null,null,'".$title."','".$area."','".$content."','".$view."','".$bed."','".$traffic."','".$expense."','".$num."',0,'".$costdate."','0','','".$discuz_uid."',now());";
    $sql = "insert into pcd_travel(id,fid,title,address,area,content,view,bed,traffic,expense,num,hot,mark,type,costdate,backdate,state,reason,postuser,timestamp) values(null,0,'" . $travel [0] . "','" . $travel [12] . "','" . $travel [1] . "','" . $travel [8] . "','" . $travel [2] . "','" . $travel [3] . "','" . $travel [4] . "','" . $travel [5] . "','" . $travel [6] . "',0,0,'" . $travel [11] . "',now(),now(),'0','','" . $travel [9] . "',now());";
    mysql_query($sql);
    $lastId = mysql_insert_id();
    $sql = "update pcd_travel set fid = " . $lastId . " where id = " . $lastId;
    mysql_query($sql);
    $sql = "update pcd_travel_route set travelid = " . $lastId . ",type='travel' where travelid = 0 and travelname = '" . $travel [0] . "'";
    mysql_query($sql);
    return $lastId;
}

function deleteTravel($id) {
    $sql = "select src,src_small from pcd_travel t,pcd_upload_travel u where t.fid = u.fid and t.fid = " . $id;
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $src = $rs->src;
        $src_small = $rs->small;
        if (file_exists($src)) {
            unlink($src);

            //echo "<br>exists.";
        }
        if (file_exists($src_small)) {
            unlink($src_small);

            //echo "<br>exists.";
        }
    }

    $sql = "select * from pcd_travel where fid = " . $id;
    $result = mysql_query($sql);
    while (($rs = mysql_fetch_object($result)) != false) {
        $subid = $rs->id;
        $subsql = "delete from pcd_upload_travel where fid = " . $subid;
        //			echo "<br>".$subsql;
        mysql_query($subsql);
    }

    $sql = "delete from pcd_travel where fid = " . $id . " or id = " . $id;
    //	echo "<br>".$sql;
    mysql_query($sql);

    $sql = "update pcd_travel_route set travelid = 0 where  travelid = " . $id;
    mysql_query($sql);

    $sql = "delete from pcd_photo_face where albumid = " . $id . " or rootid = " . $id;
    //	echo "<br>".$sql;	
    mysql_query($sql);
}

// batch delete travel
function batchDelTravel($ids) {
    $sql = "delete from pcd_travel where id in (" . $ids . ")";
    mysql_query($sql);
}

function getTravelByRoute($routeid) {
    $array = array();
    $sql = "select * from pcd_travel t,pcd_travel_route r where r.routeid = " . $routeid . " and  t.id = r.travelid";
    $array = getTravelArray($sql);
    return $array;
}

function editMyTravel($travel) {
    //		$travel = array(addslashes($title),addslashes($area),addslashes($view),addslashes($bed),addslashes($traffic),addslashes($expense),addslashes($num),addslashes($costdate),addslashes($content),addslashes($discuz_uid),addslashes($backdate),addslashes($type),$id);
    $sql = "update pcd_travel set title='" . $travel [0] . "', area='" . $travel [1] . "', content ='" . $travel [8] . "', view ='" . $travel [2] . "', bed ='" . $travel [3] . "', traffic='" . $travel [4] . "', expense='" . $travel [5] . "', num='" . $travel [6] . "', type ='" . $travel [11] . "', postuser ='" . $travel [9] . "', address='" . $travel [13] . "',timestamp=now() where id =" . $travel [12] . "";
    //	echo $sql;		
    mysql_query($sql);
}

?>